package com.helpyoutimely.order.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.helpyoutimely.R;
import com.helpyoutimely.entity.Order;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/4/14.
 */
public class OrderListAdapter extends BaseAdapter {
    private List<Order> orderList;
    Context context;

    public OrderListAdapter(Context context,List<Order> orderList){
        this.orderList = orderList;
        this.context = context;
    }
    @Override
    public int getCount(){
        return orderList.size();
    }

    @Override
    public Object getItem(int position){
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position){
        return position;
    }

    public class ViewHolder{
        TextView orderStatus;
        TextView orderDate;
        TextView orderName;
        TextView orderDetail;
        TextView orderTimer;
        TextView orderPrice;
        ImageView orderimg;
        Button orderAskButton;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){

         ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.order_item, null);
            viewHolder = new ViewHolder();
            viewHolder.orderStatus = (TextView) convertView.findViewById(R.id.orderstatus);
            viewHolder.orderDate = (TextView) convertView.findViewById(R.id.orderdate);
            viewHolder.orderName = (TextView) convertView.findViewById(R.id.ordername);
            viewHolder.orderDetail = (TextView) convertView.findViewById(R.id.orderdetail);
            viewHolder.orderTimer = (TextView) convertView.findViewById(R.id.ordertimer);
            viewHolder.orderPrice = (TextView) convertView.findViewById(R.id.orderprice);
            viewHolder.orderimg = (ImageView) convertView.findViewById(R.id.orderimg);
            viewHolder.orderAskButton = (Button) convertView.findViewById(R.id.orderaskbutton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        Order order = (Order)getItem(position);
        Date date = order.getOrderDate();
        viewHolder.orderStatus.setText(order.getOrderStatus());
        viewHolder.orderDate.setText(date.toLocaleString());
        viewHolder.orderName.setText(order.getOrderName());
        viewHolder.orderDetail.setText(order.getOrderDetail());
        viewHolder.orderTimer.setText(order.getOrderTimer());
        viewHolder.orderPrice.setText(order.getOrderPrice());
        viewHolder.orderimg.setImageResource(order.getOrderImgId());
        viewHolder.orderAskButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createDialog("15625016803");
            }
        });

        return convertView;
    }

    private void createDialog(final String phone){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("系统提示");
        builder.setMessage("是否拨打电话,电话接通以后开始计时");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //                Toast.makeText(mContext,"拨打电话",Toast.LENGTH_SHORT).show();
                Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone));
                context.startActivity(i);


            }
        });
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });
        builder.show();
    }

}
